//
//  MYGSystemMessageController.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/2/29.
//

import UIKit

class MYGSystemMessageController: MYGBaseController {
    // MARK: - Property
    var contents: [MYGSystemMessageSection] = []
    
    private var type: MYGSystemMessageType = .system
    var isShowSchedule:Bool = true
    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        configUI()
        
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.configItem()
    }
    // MARK: - 配置
  
    private func configItem() {
        
        contents.removeAll()
        MYGLoading.show()
        MMYGNetwork<MYGMessageApi, MYGPageModel<MYGSysMessageModel>>.request(target: .getMessageList(types: type.typeStr)) { [weak self] item in
            MYGLoading.hide()
            guard let items = item?.list else {
                return
            }
            
            var unReadSection = MYGSystemMessageSection(items: [], name: "未读")
            var readSection = MYGSystemMessageSection(items: [], name: "已读")
            
            for model in items {
                if model.status == 0 {// 未读
                    var image: String = "system"
                    var name: String = "系统消息"
                    if let type = self?.type {
                        image = type.image
                        name = type.name
                    }
                    let item = MYGChatListCellModel(image: image, name: name, content: model.message, time: model.updated_at,id:model.id, model: model)
                    unReadSection.items.append(item)
                    
                }else {
                    
                    var image: String = "system"
                    var name: String = "系统消息"
                    if let type = self?.type {
                        image = type.image
                        name = type.name
                    }
                    let item = MYGChatListCellModel(image: image, name: name, content: model.message, time: model.updated_at,id:model.id, model: model)
                    readSection.items.append(item)
                    
                }
            }
            
            if unReadSection.items.count > 0 {
                self?.contents = [unReadSection]
            }
            
            if readSection.items.count > 0 {
                self?.contents.append(readSection)
            }
            self!.tableView.reloadData()
            
        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
        
    }

    // MARK: - UI
    private func configUI() {
        self.title = "系统消息"
        view.addSubview(tableView)
        
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    // MARK: - lazy load
    private lazy var tableView: UITableView = {
        let tableV = UITableView()
        tableV.tableHeaderView = tableHeaderView
        tableV.delegate = self
        tableV.dataSource = self
        tableV.separatorStyle = .none
        tableV.register(cellWithClass: MYGSystemMessageCell.self)
        tableV.register(headerFooterViewClassWith: MYGMineSectionHeaderView.self)
        tableV.backgroundColor = .rgba(250, 250, 250, 1)
        return tableV
    }()
    
    private lazy var tableHeaderView: MYGSystemMessageHeaderView = {
        let view = MYGSystemMessageHeaderView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 120))
        view.isShowSchedule = isShowSchedule
        view.typeChange = {[weak self] type in
            self?.type = type
            self!.configItem()
        }
        return view
    }()
}

extension MYGSystemMessageController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        contents.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        contents[section].items.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: MYGSystemMessageCell.self, for: indexPath)
        cell.model = contents[indexPath.section].items[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        52
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = tableView.dequeueReusableHeaderFooterView(withClass: MYGMineSectionHeaderView.self)
        view.nameLabel.text = contents[section].name
        return view
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       let model = contents[indexPath.section].items[indexPath.row]
        
        MMYGNetwork<MYGMessageApi,MYGEmptyModel>.request(target: .redMessage(msg_id: model.model!.id)) { _ in
            if model.model?.type == 1 {
                let vc = MYGApplyFriendViewController()
                vc.model = model.model
                self.navigationController?.pushViewController(vc, animated: true)
            }else if (model.model?.type ==  6){
                let vc = MYGTriageViewController()
                vc.model = model.model
                self.navigationController?.pushViewController(vc, animated: true)
            }
        } failed: { _, error in
            
        }

    }
}
